default_platform(:ios)

DEVELOPER_APP_ID = ENV["DEVELOPER_APP_ID"]
DEVELOPER_APP_IDENTIFIER = ENV["DEVELOPER_APP_IDENTIFIER"]
DEVELOPER_APP_EXTENSION_IDENTIFIER = ENV["DEVELOPER_APP_EXTENSION_IDENTIFIER"]
TEMP_KEYCHAIN_USER = ENV["TEMP_KEYCHAIN_USER"]
TEMP_KEYCHAIN_PASSWORD = ENV["TEMP_KEYCHAIN_PASSWORD"]
APPLE_ISSUER_ID = ENV["APPLE_ISSUER_ID"]
APPLE_KEY_ID = ENV["APPLE_KEY_ID"]
APPLE_KEY_CONTENT = ENV["APPLE_KEY_CONTENT"]
GIT_AUTHORIZATION = ENV["GIT_AUTHORIZATION"]

JUDGE0_KEY = ENV["JUDGE0_KEY"]
JUDGE0_ENDPOINT = ENV["JUDGE0_ENDPOINT"]
DISTRIBUTE_EXTERNAL = ENV["DISTRIBUTE_EXTERNAL"] == "true"
XCODE_PATH = ENV["XCODE_PATH"]

def delete_temp_keychain(name)
  delete_keychain(
    name: name
  ) if File.exist? File.expand_path("~/Library/Keychains/#{name}-db")
end

def create_temp_keychain(name, password)
  create_keychain(
    name: name,
    password: password,
    unlock: false,
    timeout: 0
  )
end

def ensure_temp_keychain(name, password)
  delete_temp_keychain(name)
  create_temp_keychain(name, password)
end

platform :ios do
  lane :beta do
    xcode_select(XCODE_PATH)

    keychain_name = TEMP_KEYCHAIN_USER
    keychain_password = TEMP_KEYCHAIN_PASSWORD
    ensure_temp_keychain(keychain_name, keychain_password)

    changelog = changelog_from_git_commits(pretty: "%as - %s")

    api_key = app_store_connect_api_key(
      key_id: APPLE_KEY_ID,
      issuer_id: APPLE_ISSUER_ID,
      key_content: APPLE_KEY_CONTENT,            
      duration: 1100,            
      in_house: false
    )
    
    increment_build_number(
      xcodeproj: "Code.xcodeproj",
      build_number: latest_testflight_build_number + 1
    )

    Dir.chdir("..") do
      sh("./downloadFrameworks.sh")
      sh(%Q[echo 'let JUDGE0_ENDPOINT = "#{JUDGE0_ENDPOINT}"' > CodeApp/Constants/Secrets.swift])
      sh(%Q[echo 'let JUDGE0_KEY = "#{JUDGE0_KEY}"' >> CodeApp/Constants/Secrets.swift])
    end

    # run_tests(scheme: "Code UI")

    match(
      type: 'appstore',
      app_identifier: ["#{DEVELOPER_APP_IDENTIFIER}", "#{DEVELOPER_APP_EXTENSION_IDENTIFIER}"],
      git_basic_authorization: Base64.strict_encode64(GIT_AUTHORIZATION),
      readonly: true,
      keychain_name: keychain_name,
      keychain_password: keychain_password,
      api_key: api_key
    )

    gym(
      configuration: "Release",
      project: "Code.xcodeproj",
      scheme: "Code App",
      export_method: "app-store",
    )

    pilot(
      apple_id: "#{DEVELOPER_APP_ID}",
      app_identifier: "#{DEVELOPER_APP_IDENTIFIER}",
      distribute_external: DISTRIBUTE_EXTERNAL,
      notify_external_testers: DISTRIBUTE_EXTERNAL,
      ipa: "./Code.ipa",
      changelog: "GitHub Action automated build. For detailed changes, " + 
      "visit https://github.com/thebaselab/codeapp.\n" + 
      "\nGenerated Changelog:\n" + changelog,
      groups: (DISTRIBUTE_EXTERNAL ? ["Public"] : []),
      wait_processing_interval: 300
    )
    

    delete_temp_keychain(keychain_name)
  end

  lane :tests do
    xcode_select(XCODE_PATH)
    Dir.chdir("..") do
      sh("./downloadFrameworks.sh")
    end
    run_tests(scheme: "Code UI")
  end
end